﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao.Intermediarios
{
    public class IPOrdenCompra:Intermediario
    {
         public override void save<T>(T entity)
        {
            try 
	        {
                OrdenCompra oc = entity as OrdenCompra;
                dao.OrdenCompras.InsertOnSubmit(oc);		
	        }
	        catch (Exception)
	        {		
		        throw;
	        }
        }

        public override List<T> getAll<T>()
        {
            return (from oc in dao.OrdenCompras select oc).ToList() as List<T>;
        }

        public override List<T> getByCriterio<T>(string att, string op, string val)
        {
            throw new NotImplementedException();
        }

        public override IList<T> getCriterioById<T>(string att, string op, int val)
        {
            return (from oc in dao.OrdenCompras where oc.nroOrdenCompra== Convert.ToInt16(val) select oc).ToList() as List<T>;
        }

        public IList<T> getOrdenCompras<T>(string att, string op, int val)
        {
            return (from oc in dao.OrdenCompras where oc.idOC == Convert.ToInt16(val) select oc).ToList() as List<T>;
        }

        public override object getPorId(int id)
        {
            throw new NotImplementedException();
        }

        public override IList<T> getPorCriterio<T>(string name)
        {
            throw new NotImplementedException();
        }

        public override void Update<T>(T entity)
        {
           OrdenCompra oc = entity as OrdenCompra;

            //dao.OrdenCompras.InsertOnSubmit(oc);
            try
            {
               //update OrdenCompra set estado='completada' where idOC=1;
               dao.ExecuteQuery<OrdenCompra>("Update OrdenCompra set estado='" + oc.estado.ToString()+"' where idOC="+ Convert.ToInt16(oc.idOC)+";"); 
            }
            catch (Exception)
            {
                throw;
               
            }
        }

        public override void Delete(int id)
        {
            try
            {
                var ct = (from oc in dao.OrdenCompras where oc.nroOrdenCompra == id select oc).SingleOrDefault();

                if (ct != null)
                {
                  //  ct.Activo = false;
                    dao.OrdenCompras.DeleteOnSubmit(ct);
                    dao.SubmitChanges();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
